{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "#Assignment #2\n", "##Blank, Fall 2014\n", "\n", "**Due**: Please bring two hardcopies to class Thursday, Sept 18, 2014\n", "\n", "In the following problems, please define the function, and test it to make sure it works. I have provided a few examples, but you should test your function thoroughly to make sure that it works.\n", "\n", "If it doesn't work in some instances, you should demonstrate where it fails. Otherwise, I will assume that you don't know that it fails." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Problem: remove-first" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`remove-first` takes an item and a list, and removes the first occurence of item in the list." ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(define remove-first\n", " (lambda (item lyst)\n" ] }, { "cell_type": "code", "execution_count": 53, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-first 'a '())" ] }, { "cell_type": "code", "execution_count": 54, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-first 'a '(a))" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(a a)" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-first 'a '(a a a))" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(b c)" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-first 'a '(b c a))" ] }, { "cell_type": "code", "execution_count": 57, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(a b c a)" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-first 'a '(a a b c a))" ] }, { "cell_type": "code", "execution_count": 58, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(b c d)" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-first 'a '(b c d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. Problem: remove-all" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`remove-all` takes an item and a list and removes all occurences of item from the list." ] }, { "cell_type": "code", "execution_count": 59, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(define remove-all\n", " (lambda (item lyst)\n" ] }, { "cell_type": "code", "execution_count": 60, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-all 'a '())" ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-all 'a '(a))" ] }, { "cell_type": "code", "execution_count": 62, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-all 'a '(a a a))" ] }, { "cell_type": "code", "execution_count": 63, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(b c d)" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-all 'a '(b a c a d a))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. Problem: remove-nth" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`remove-nth` takes a number (1-based indexing), an item, and a list. It removes the nth instance of item in the list. " ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(define remove-nth\n", " (lambda (n item lyst)\n" ] }, { "cell_type": "code", "execution_count": 65, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(b a c a d)" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-nth 1 'a '(a b a c a d))" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(a b c a d)" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-nth 2 'a '(a b a c a d))" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(a b a c d)" ] }, "execution_count": 67, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(remove-nth 3 'a '(a b a c a d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4. Problem: substitute" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`substitute` takes an item to search for, an item to replace it, and a list. The function will find all instances of the old item and replace it with the new item." ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(define substitute\n", " (lambda (old new lyst)\n" ] }, { "cell_type": "code", "execution_count": 69, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(substitute 'old 'new '())" ] }, { "cell_type": "code", "execution_count": 70, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(new)" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(substitute 'old 'new '(old))" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(new new a b c new)" ] }, "execution_count": 71, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(substitute 'old 'new '(old old a b c old))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 5. Problem: substitute*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`substitute*` works just like `substitute` but will find all instances no matter how deeply hidden they are inside sublists of the given list." ] }, { "cell_type": "code", "execution_count": 98, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(define substitute*\n", " (lambda (old new lyst)\n" ] }, { "cell_type": "code", "execution_count": 99, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "((new) ((((new apple bannan)) (a b c new d e))) new test ((word)) (new) new)" ] }, "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(substitute* 'old 'new '((old)((((old apple bannan)) (a b c old d e))) old test ((word)) (old) old))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 6. Problem: infix->prefix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`infix->prefix` will convert expressions given in infix notation to prefix notation. It gives an error if you attempt to operate on an empty list." ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(define infix->prefix\n", " (lambda (expr)\n", " (cond\n", " ((null? expr) (error 'infix->prefix \"Cannot process an empty expression\"))\n" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(infix->prefix 1)" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(+ 1 2)" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(infix->prefix '(1 + 2))" ] }, { "cell_type": "code", "execution_count": 81, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "(* (+ 1 2) (/ 8 9))" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(infix->prefix '((1 + 2) * (8 / 9)))" ] }, { "cell_type": "code", "execution_count": 82, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "Traceback (most recent call last):\n", " File \"stdin\", line 1, col 1, in 'infix->prefix'\n", " File \"stdin\", line 4, col 20, in 'error'\n", " File \"stdin\", line 4, col 20\n", "RunTimeError: Error in 'infix->prefix': Cannot process an empty expression\n", "\n" ] } ], "source": [ "(infix->prefix '())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 7. Problem: eval-infix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`eval-infix` will evaluate infix expressions. It should handle +, *, /, and -. It should give an error otherwise." ] }, { "cell_type": "code", "execution_count": 92, "metadata": { "collapsed": false }, "outputs": [], "source": [ "(define eval-infix\n", " (lambda (e)\n" ] }, { "cell_type": "code", "execution_count": 95, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 95, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(eval-infix 42)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 93, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(eval-infix '(1 + 1))" ] }, { "cell_type": "code", "execution_count": 96, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 96, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(eval-infix '(2 * (3 + 7)))" ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "80/9" ] }, "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(eval-infix '((8 / 9) * (3 + 7)))" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "\n", "Traceback (most recent call last):\n", " File \"stdin\", line 1, col 1, in 'eval-infix'\n", " File \"stdin\", line 10, col 15, in 'error'\n", " File \"stdin\", line 10, col 15\n", "RunTimeError: Error in 'eval-infix': Unknown operator ^\n", "\n" ] } ], "source": [ "(eval-infix '(1 ^ 1))" ] } ], "metadata": { "kernelspec": { "display_name": "Calysto Scheme 2", "language": "scheme", "name": "calysto_scheme" }, "language_info": { "codemirror_mode": { "name": "scheme" }, "mimetype": "text/x-scheme", "name": "scheme", "pygments_lexer": "scheme" } }, "nbformat": 4, "nbformat_minor": 0 }